package test.yzx.pproject.util;


import android.view.View;
import android.widget.ExpandableListView;

public abstract class StickyExpandableListAdapter extends android.widget.BaseExpandableListAdapter{

    /** return a view as same as "getGroupView(int groupPosition)" as sticky header, include event */
    public abstract View getCopyGroupView(int groupPosition);





    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }




    /** To judge weather the position is a group view */
    public boolean isGroupViewType(int position, ExpandableListView listview){
        if(isEmpty()) return false;
        int headerCount = listview.getHeaderViewsCount();
        if(position<headerCount) return false;
        position -= headerCount;
        int totalCount = 0;
        for (int i = 0, groupCount = getGroupCount(); i < groupCount; i++) {
            if(position == totalCount)
                return true;
            if(listview.isGroupExpanded(i))
                for (int j = 0, childrenCount = getChildrenCount(i); j < childrenCount; j++) {
                    totalCount++;
                    if(position==totalCount)
                        return false;
                }
            totalCount++;
        }
        return false;
    }
    /** get group position by position */
    public int getGroupPositionByPosition(int position, ExpandableListView listview){
        if(isEmpty()) return -1;
        int headerCount = listview.getHeaderViewsCount();
        if(position<headerCount) return -1;
        position -= headerCount;
        int totalCount = 0;
        for (int i = 0, groupCount = getGroupCount(); i < groupCount; i++) {
            if(position == totalCount)
                return i;
           if(listview.isGroupExpanded(i))
               for (int j = 0, childrenCount = getChildrenCount(i); j < childrenCount; j++) {
                   totalCount++;
                   if(position==totalCount)
                       return i;
               }
            totalCount++;
        }
        return -1;
    }
    /** get child position by position */
    public int getChildPositionByPosition(int position, ExpandableListView listview){
        if(isEmpty()) return -1;
        int headerCount = listview.getHeaderViewsCount();
        if(position<headerCount) return -1;
        position -= headerCount;
        int totalCount = 0;
        for (int i = 0, groupCount = getGroupCount(); i < groupCount; i++) {
            if(position == totalCount)
                return -2;
            if(listview.isGroupExpanded(i))
                for (int j = 0, childrenCount = getChildrenCount(i); j < childrenCount; j++) {
                    totalCount++;
                    if(position==totalCount)
                        return j;
                }
            totalCount++;
        }
        return -1;
    }


}
